package com.example.propertymangesystem.controller;

import com.example.propertymangesystem.controller.util.R;
import com.example.propertymangesystem.domain.employee;
import com.example.propertymangesystem.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/employees")
public class employeeController {
    @Autowired
    private IEmployeeService employeeService;

    //获取全部
    @GetMapping
    public R getAll() {
        return new R(true, employeeService.list(null));
    }

    //为部门管理的主管绑定数据源，查某个部门
    @GetMapping("/{depart}/{employee}/{did}")
    public R getDepartmentAll(@PathVariable Integer did) {
        return new R(true, employeeService.getDepartmentAll(did));
    }

    //分页查询
    @GetMapping("/{current}/{size}")
    public R getPage(@PathVariable Integer current, @PathVariable Integer size, employee employee){
        return new R(true, employeeService.getPage(current, size, employee));
    }

    //根据id查询
    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id){
        employee employee = employeeService.getById(id);
        boolean b = false;
        if(employee != null) b = true;
        return new R(b, employee);
    }

    //修改数据
    @PutMapping
    public R update(@RequestBody employee employee){
        boolean b = employeeService.updateById(employee);
        return new R(b, b ? "修改成功^_^":"修改失败^…^");
    }

    //添加
    @PostMapping
    public R save(@RequestBody employee employee){
        boolean b = employeeService.save(employee);
        return new R(b, b ? "添加成功^_^":"添加失败^…^");
    }

    //删除
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        boolean b = employeeService.removeById(id);
        return new R(b, b ? "删除成功^_^":"删除失败^…^");
    }

}
